package com.zyf.project.capchain.dto;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.zyf.project.capchain.model.Good;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author Zhou Yifan
 */
@Data
@TableName(value = "good")
public class GoodDTO implements Serializable {

    /**
     * 商品主键，采用雪花算法生成
     */
    @TableId(value = "gid", type = IdType.ASSIGN_ID)
    private Long gid;

    /**
     * 商品识别码
     */
    private String idCode;

    /**
     * 商品名字
     */
    private String name;

    /**
     * 商品价格
     */
    private Double price;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdTime;

    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime modifiedTime;

    public GoodDTO() {
        this.createdTime = LocalDateTime.now();
        this.modifiedTime = LocalDateTime.now();
    }

    /**
     * DTO 转 Model
     *
     * @return Good
     */
    public Good toModel() {
        Good good = new Good();
        good.setGid(gid);
        good.setIdCode(idCode);
        good.setName(name);
        good.setPrice(price);
        good.setCreatedTime(createdTime);
        good.setModifiedTime(modifiedTime);
        return good;
    }

}
